package com.neulogistics.contrib.usercenter.domain.model.enumeration;

import com.neulogistics.contrib.usercenter.domain.exception.UserCenterDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class UserStatus extends Enumeration {


    /**
     * 用户状态: 0:关闭；1:开启；
     */
    public static final int Disable_Id = 0;
    public static final int Enable_Id = 1;

    public static final UserStatus Disable = new UserStatus(Disable_Id, "Disable");
    public static final UserStatus Enable = new UserStatus(Enable_Id, "Enable");


    private static final List<UserStatus> list = Arrays.asList(
            Disable,
            Enable
    );
    protected UserStatus(int id, String name) {
        super(id, name);
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }

    public static List<UserStatus> getList(){return list;}

    public static UserStatus fromName(String name) throws UserCenterDomainException {
        Optional<UserStatus> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()){
            return optional.get();
        }
        else {
            throw new UserCenterDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            UserStatus.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }
}
